// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import static org.hypirinha.html.codegen.CodegenUtil.capitalise;
import static org.hypirinha.html.codegen.CodegenUtil.removeSpecialCharacters;

import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Collection;

public class AttributeFactory {
    private SortedSet<String> attributes = new TreeSet<String>();

    public AttributeFactory(Collection<String> attributes) {
        this.attributes.addAll(attributes);
    }

    public GeneratedClass createImplementation() {
        GeneratedClass generatedClass = new GeneratedClass("org.hypirinha.html", "AttributeFactory");
        generatedClass.addImport("org.hypirinha.html.attributes.*");
        for (String attribute : attributes) {
            String attributeName = capitalise(removeSpecialCharacters(attribute)) + "Attribute";
            String methodName = avoidKeywwordClash(removeSpecialCharacters(attribute));
            ConcreteMethod method = new ConcreteMethod(methodName, attributeName, "String value",
                    "return new " + attributeName + "(value);");
            method.getModifiers().add("static");
            generatedClass.getMembers().add(method);
        }
        return generatedClass;
    }

    private String avoidKeywwordClash(String name) {
        if (name.equals("char")) return "charr";
        if (name.equals("class")) return "classs";
        if (name.equals("for")) return "forr";
        return name;
    }
}
